package com.siqi.hr.system.common.model;

import com.siqi.hr.system.common.utils.Md5Utils;
import com.siqi.hr.system.common.utils.intf.BaseEnum;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户可登陆模块对象类型
 */
public enum ModuleType implements BaseEnum<Integer> {

    SETTING(1, "系统设置", "1_"),

    MARKETING(2, "业务管理", "_1");

    /**
     * 模块编号
     */
    private int code;
    /**
     * 模块中文名称
     */
    private String desc;

    private String moduleMark;

    public static String NO_MODULE = "00";

    public static Integer[] MODULE_AUTHORITY = { SETTING.getCode(), MARKETING.getCode()};

    private ModuleType(int code, String desc, String moduleMark) {
        this.code = code;
        this.desc = desc;
        this.moduleMark = moduleMark;
    }

    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    public String getModuleMark() {
        return moduleMark;
    }

    public static List<Module> getAllModule() {
        List<Module> modulelist = new ArrayList<Module>();
        modulelist.add(new Module(SETTING.getCode(), SETTING.name(), SETTING.getModuleMark(), SETTING.getDesc(),false));
        modulelist.add(new Module(MARKETING.getCode(), MARKETING.name(),MARKETING.getModuleMark(), MARKETING.getDesc(),true));
        return modulelist;
    }

    /**
     * 根据可登陆模块id返回模块枚举对象(xiaogq)
     * @param moduleid
     * @return
     */
    public static ModuleType getModuleType(Integer moduleid) {
        switch (moduleid.intValue()) {
            case 1:
                return SETTING;
            case 2:
                return MARKETING;
        }
        throw new IllegalArgumentException("模块不存在");
    }

    public static List<String> getCRMModuleMark() {
        List<String> markList = new ArrayList<String>();
        markList.add(MARKETING.getModuleMark());
        return markList;
    }

    public static Integer getModuleCodeFromMd5(String md5Module) {
        Integer module = null;
        if (StringUtils.isNotEmpty(md5Module)) {
            if (md5Module.equals(Md5Utils.Md5(SETTING.getCode().toString())))
                module = SETTING.getCode();
            else if (md5Module.equals(Md5Utils.Md5(MARKETING.getCode().toString())))
                module = MARKETING.getCode();
            else {
                module = null;
            }
        }
        return module;
    }

    public static List<ModuleType> getModuleList(String module) {
        List<ModuleType> list = new ArrayList<ModuleType>();
        char[] marks = module.toCharArray();
        int moduleid = 1;
        for (char mark : marks) {
            if (mark == '1') {
                list.add(getModuleType(moduleid));
            }
            moduleid++;
        }
        return list;
    }

    public static String getModuleStr(String module) {
        if ((StringUtils.isBlank(module)) || (StringUtils.equals(module, "null"))) {
            return "";
        }
        List<String> moduleStr = new ArrayList<String>();
        List<ModuleType> list = getModuleList(module);
        for (ModuleType mt : list) {
            moduleStr.add(mt.getDesc());
        }
        return StringUtils.join(moduleStr, ", ");
    }

}
